package edu.unh.sustain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Displays route 3A of Wildcat Transit as an expandable list.  This means
 * that each stop will appear as a top level "group", and after it is touched
 * it will expand to show the different times that the bus will service
 * that stop.
 * 
 * Displays schedule for route 3A on weekdays, weekends, and reduced service.
 * @author Blake Janelle
 *
 */
public class Route3aActivity extends ExpandableListActivity {
	
	String timeframe;
	Context ct = this;
	int tabNum = 0;
	int spinnerState = 0;
	HashMap<String,Integer> runs = new HashMap<String,Integer>();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    timeframe = getTimeframe(savedInstanceState);
	    ArrayAdapter<CharSequence> adapter = null;
	    this.addStringRuns();
	    setContentView(R.layout.route3ab);
	    
	    // set up the spinner that controls the current timeframe
	    adapter = ArrayAdapter.createFromResource(
	            this, R.array.r3_timeframes, android.R.layout.simple_spinner_item);
	    Spinner spinner = (Spinner) findViewById(R.id.timeframes);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	    
	    // set up an expandable list that groups every stop, and when pressed, expands to display each stop time
	    SimpleExpandableListAdapter lstAdapter =
				new SimpleExpandableListAdapter
				(
					this,
					createGroupList(), 						// Creating group List.
					R.layout.expand_group_row,				// Group item layout XML.
					new String[] { "Group Item" },			// the key of group item.
					new int[] { R.id.row },				// ID of each group item.-Data under the key goes into this TextView.
					createChildList(),						// childData describes second-level entries.
					R.layout.expand_child_row,				// Layout for sub-level entries(second level).
					new String[] {"Sub Item"},				// Keys in childData maps to display.
					new int[] { R.id.grp_child }			// Data under the keys above go into these TextViews.
				);
					
	    ExpandableListView a = (ExpandableListView)findViewById(android.R.id.list);
	    a.setAdapter(lstAdapter);
	    
	    // set text box labels for the corresponding timeframe
	    if( timeframe.equals("weekends"))
	    {
	    	spinnerState = 1;
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.a3w_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.a3w_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.a3w_range);
		    spinner.setSelection(spinnerState);
	    }
	    else if( timeframe.equals("reduced"))
	    {
	    	spinnerState = 2;
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.a3r_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.a3r_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.a3r_range);
		    spinner.setSelection(spinnerState);
	    }
	    else
	    {
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.a3_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.a3_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.a3_range);
		    spinner.setSelection(spinnerState);
	    }
	}
	
	/**
	 * Create list of stops which are the "groups" of the expandable list
	 * 
	 * @return List<HashMap<String,String>>
	 */
	private List<HashMap<String,String>> createGroupList()
	{
		String[] stops;
				
		if(timeframe.equals("weekdays"))
			stops = getResources().getStringArray(R.array.a3stops);
		else if(timeframe.equals("weekends"))
			stops = getResources().getStringArray(R.array.a3wstops);
		else
			stops = getResources().getStringArray(R.array.a3rstops);
		
		ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
		  
		for( int i = 0 ; i < stops.length; i++ )
		{
			HashMap<String,String> m = new HashMap<String,String>();
			m.put( "Group Item", stops[i] );
			result.add( m );
		}
		
		return (List<HashMap<String,String>>)result;
    }

	/**
	 * Create list of times for each stop.
	 * 
	 * @return List<ArrayList<HashMap<String,String>>>
	 */
	private List<ArrayList<HashMap<String,String>>> createChildList()
	{
		String[] stops;
		String prefix;
		
		if(timeframe.equals("weekdays"))
		{
			stops = getResources().getStringArray(R.array.a3stops);
			prefix = "a3stop";
		}
		else if(timeframe.equals("weekends"))
		{
			stops = getResources().getStringArray(R.array.a3wstops);
			prefix = "a3wstop";
		}
		else
		{
			stops = getResources().getStringArray(R.array.a3rstops);
			prefix = "a3rstop";
		}
		
		ArrayList<ArrayList<HashMap<String,String>>> result = new ArrayList<ArrayList<HashMap<String,String>>>();
		
		for( int i = 0 ; i < stops.length; i++ )
		{
			String index = prefix + (i+1);
			String[] run = getResources().getStringArray(runs.get(index));
			ArrayList<HashMap<String,String>> secList = new ArrayList<HashMap<String,String>>();
			
			for( int n = 0 ; n < run.length ; n++ )
			{
				HashMap<String,String> child = new HashMap<String,String>();
				child.put( "Sub Item", run[n] );
				secList.add( child );
			}
			
			result.add( secList );
		}
		return result;
	}
	
	/**
	 * Extracts the timeframe data from the Instance and returns it
	 * 
	 * @param savedInstanceState previous state
	 * @return the string or null
	 */
	private String getTimeframe(Bundle savedInstanceState)
	{
		String ret = null;
		
		if( savedInstanceState != null )
	    {
	    	ret = (String) savedInstanceState.getString("timeframe");
	    }
	    else
	    {
	    	Bundle extras = getIntent().getExtras();
	    	
	    	if( extras != null )
	    		ret = extras.getString("timeframe");
	    }
		
		return ret;
	}
	
	/**
	 * Returns the Sting equivalent of the spinner position
	 * @param current spinner position
	 * @return String equivalent
	 */
	private String getPos(int current)
	{
		if(current == 0)
			return new String("weekdays");
		else if(current == 1)
			return new String("weekends");
		else
			return new String("reduced");
	}
	
	/**
	 * Saves the state of the screen.  This is called on orientation changes. 
	 */
	public void onSaveInstanceState(Bundle savedInstanceState)
	{
		  savedInstanceState.putString("timeframe", timeframe);
		  super.onSaveInstanceState(savedInstanceState);
	}
	
	/**
	 * A "run array" is a string array of times when each
	 * stop is serviced.  Each run array id (references route3.xml)
	 * is added to the runs HashMap so that these array ids can 
	 * be referenced as strings in createGroupList() and 
	 * createChildList().
	 */
	private void addStringRuns()
	{
		// weekday stops
		runs.put( "a3stop1", R.array.a3stop1);
	    runs.put( "a3stop2", R.array.a3stop2);
	    runs.put( "a3stop3", R.array.a3stop3);
	    runs.put( "a3stop4", R.array.a3stop4);
	    runs.put( "a3stop5", R.array.a3stop5);
	    runs.put( "a3stop6", R.array.a3stop6);
	    runs.put( "a3stop7", R.array.a3stop7);
	    runs.put( "a3stop8", R.array.a3stop8);
	    runs.put( "a3stop9", R.array.a3stop9);
	    runs.put( "a3stop10", R.array.a3stop10);
	    runs.put( "a3stop11", R.array.a3stop11);
	    runs.put( "a3stop12", R.array.a3stop12);
	    runs.put( "a3stop13", R.array.a3stop13);
	    runs.put( "a3stop14", R.array.a3stop14);
	    runs.put( "a3stop15", R.array.a3stop15);
	    runs.put( "a3stop16", R.array.a3stop16);
	    runs.put( "a3stop17", R.array.a3stop17);
	    runs.put( "a3stop18", R.array.a3stop18);
	    runs.put( "a3stop19", R.array.a3stop19);
	    runs.put( "a3stop20", R.array.a3stop20);
	    runs.put( "a3stop21", R.array.a3stop21);
	    runs.put( "a3stop22", R.array.a3stop22);
	    runs.put( "a3stop23", R.array.a3stop23);
	    runs.put( "a3stop24", R.array.a3stop24);
	    runs.put( "a3stop25", R.array.a3stop25);
	    runs.put( "a3stop26", R.array.a3stop26);
	    runs.put( "a3stop27", R.array.a3stop27);
	    runs.put( "a3stop28", R.array.a3stop28);
	    runs.put( "a3stop29", R.array.a3stop29);
	    runs.put( "a3stop30", R.array.a3stop30);
	    runs.put( "a3stop31", R.array.a3stop31);
	    runs.put( "a3stop32", R.array.a3stop32);
	    runs.put( "a3stop33", R.array.a3stop33);
	    runs.put( "a3stop34", R.array.a3stop34);
	    runs.put( "a3stop35", R.array.a3stop35);
	    runs.put( "a3stop36", R.array.a3stop36);
	    runs.put( "a3stop37", R.array.a3stop37);
	    runs.put( "a3stop38", R.array.a3stop38);
	    runs.put( "a3stop39", R.array.a3stop39);
	    runs.put( "a3stop40", R.array.a3stop40);
	    runs.put( "a3stop41", R.array.a3stop41);
	    runs.put( "a3stop42", R.array.a3stop42);
	    runs.put( "a3stop43", R.array.a3stop43);
	    runs.put( "a3stop44", R.array.a3stop44);
	    runs.put( "a3stop45", R.array.a3stop45);
	    
	    // weekend stops
	    runs.put( "a3wstop1", R.array.a3wstop1);
	    runs.put( "a3wstop2", R.array.a3wstop2);
	    runs.put( "a3wstop3", R.array.a3wstop3);
	    runs.put( "a3wstop4", R.array.a3wstop4);
	    runs.put( "a3wstop5", R.array.a3wstop5);
	    runs.put( "a3wstop6", R.array.a3wstop6);
	    runs.put( "a3wstop7", R.array.a3wstop7);
	    runs.put( "a3wstop8", R.array.a3wstop8);
	    runs.put( "a3wstop9", R.array.a3wstop9);
	    runs.put( "a3wstop10", R.array.a3wstop10);
	    runs.put( "a3wstop11", R.array.a3wstop11);
	    runs.put( "a3wstop12", R.array.a3wstop12);
	    runs.put( "a3wstop13", R.array.a3wstop13);
	    runs.put( "a3wstop14", R.array.a3wstop14);
	    runs.put( "a3wstop15", R.array.a3wstop15);
	    runs.put( "a3wstop16", R.array.a3wstop16);
	    runs.put( "a3wstop17", R.array.a3wstop17);
	    runs.put( "a3wstop18", R.array.a3wstop18);
	    runs.put( "a3wstop19", R.array.a3wstop19);
	    runs.put( "a3wstop20", R.array.a3wstop20);
	    runs.put( "a3wstop21", R.array.a3wstop21);
	    runs.put( "a3wstop22", R.array.a3wstop22);
	    runs.put( "a3wstop23", R.array.a3wstop23);
	    runs.put( "a3wstop24", R.array.a3wstop24);
	    runs.put( "a3wstop25", R.array.a3wstop25);
	    runs.put( "a3wstop26", R.array.a3wstop26);
	    runs.put( "a3wstop27", R.array.a3wstop27);
	    runs.put( "a3wstop28", R.array.a3wstop28);
	    runs.put( "a3wstop29", R.array.a3wstop29);
	    runs.put( "a3wstop30", R.array.a3wstop30);
	    runs.put( "a3wstop31", R.array.a3wstop31);
	    runs.put( "a3wstop32", R.array.a3wstop32);
	    runs.put( "a3wstop33", R.array.a3wstop33);
	    runs.put( "a3wstop34", R.array.a3wstop34);
	    runs.put( "a3wstop35", R.array.a3wstop35);
	    runs.put( "a3wstop36", R.array.a3wstop36);
	    runs.put( "a3wstop37", R.array.a3wstop37);
	    runs.put( "a3wstop38", R.array.a3wstop38);
	    runs.put( "a3wstop39", R.array.a3wstop39);
	    runs.put( "a3wstop40", R.array.a3wstop40);
	    runs.put( "a3wstop41", R.array.a3wstop41);
	    runs.put( "a3wstop42", R.array.a3wstop42);
	    runs.put( "a3wstop43", R.array.a3wstop43);
	    runs.put( "a3wstop44", R.array.a3wstop44);
	    runs.put( "a3wstop45", R.array.a3wstop45);
	    
	    // reduced service stops
	    runs.put( "a3rstop1", R.array.a3rstop1);
	    runs.put( "a3rstop2", R.array.a3rstop2);
	    runs.put( "a3rstop3", R.array.a3rstop3);
	    runs.put( "a3rstop4", R.array.a3rstop4);
	    runs.put( "a3rstop5", R.array.a3rstop5);
	    runs.put( "a3rstop6", R.array.a3rstop6);
	    runs.put( "a3rstop7", R.array.a3rstop7);
	    runs.put( "a3rstop8", R.array.a3rstop8);
	    runs.put( "a3rstop9", R.array.a3rstop9);
	    runs.put( "a3rstop10", R.array.a3rstop10);
	    runs.put( "a3rstop11", R.array.a3rstop11);
	    runs.put( "a3rstop12", R.array.a3rstop12);
	    runs.put( "a3rstop13", R.array.a3rstop13);
	    runs.put( "a3rstop14", R.array.a3rstop14);
	    runs.put( "a3rstop15", R.array.a3rstop15);
	    runs.put( "a3rstop16", R.array.a3rstop16);
	    runs.put( "a3rstop17", R.array.a3rstop17);
	    runs.put( "a3rstop18", R.array.a3rstop18);
	    runs.put( "a3rstop19", R.array.a3rstop19);
	    runs.put( "a3rstop20", R.array.a3rstop20);
	    runs.put( "a3rstop21", R.array.a3rstop21);
	    runs.put( "a3rstop22", R.array.a3rstop22);
	    runs.put( "a3rstop23", R.array.a3rstop23);
	    runs.put( "a3rstop24", R.array.a3rstop24);
	    runs.put( "a3rstop25", R.array.a3rstop25);
	    runs.put( "a3rstop26", R.array.a3rstop26);
	    runs.put( "a3rstop27", R.array.a3rstop27);
	    runs.put( "a3rstop28", R.array.a3rstop28);
	    runs.put( "a3rstop29", R.array.a3rstop29);
	    runs.put( "a3rstop30", R.array.a3rstop30);
	    runs.put( "a3rstop31", R.array.a3rstop31);
	    runs.put( "a3rstop32", R.array.a3rstop32);
	    runs.put( "a3rstop33", R.array.a3rstop33);
	    runs.put( "a3rstop34", R.array.a3rstop34);
	    runs.put( "a3rstop35", R.array.a3rstop35);
	    runs.put( "a3rstop36", R.array.a3rstop36);
	    runs.put( "a3rstop37", R.array.a3rstop37);
	    runs.put( "a3rstop38", R.array.a3rstop38);
	    runs.put( "a3rstop39", R.array.a3rstop39);
	    runs.put( "a3rstop40", R.array.a3rstop40);
	    runs.put( "a3rstop41", R.array.a3rstop41);
	    runs.put( "a3rstop42", R.array.a3rstop42);
	    runs.put( "a3rstop43", R.array.a3rstop43);
	    runs.put( "a3rstop44", R.array.a3rstop44);
	    runs.put( "a3rstop45", R.array.a3rstop45);
	}
	
	/**
	 * If the spinner item that was selected is different from the current
	 * spinner page, then it goes to the appropriate page.
	 *
	 */
	public class MyOnItemSelectedListener implements OnItemSelectedListener
	{		
	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	    	Intent i;
		    
	    	if( pos != spinnerState )
	    	{
	    		i = new Intent();
	    		i.setClass( ct, Route3Activity.class);
	    		i.putExtra("timeframe", getPos(pos));
	    		i.putExtra("tab", tabNum);
	            startActivityForResult(i, 0);
	    	}
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	    	// Do nothing.
	    }
	}
}
